package com.site.blog.my.core.service.impl;

import com.site.blog.my.core.dao.AdminUserMapper;
import com.site.blog.my.core.entity.AdminUser;
import com.site.blog.my.core.entity.BlogCategory;
import com.site.blog.my.core.entity.BlogFriends;
import com.site.blog.my.core.entity.BlogTag;
import com.site.blog.my.core.service.AdminUserService;
import com.site.blog.my.core.util.MD5Util;
import com.site.blog.my.core.util.PageQueryUtil;
import com.site.blog.my.core.util.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Override
    public AdminUser login(String userName, String password) {
        String passwordMd5 = MD5Util.MD5Encode(password, "UTF-8");
        return adminUserMapper.login(userName, passwordMd5);
    }

    @Override
    public AdminUser getUserDetailById(Integer loginUserId) {
        return adminUserMapper.selectByPrimaryKey(loginUserId);
    }

    @Override
    public Boolean updatePassword(Integer loginUserId, String originalPassword, String newPassword) {
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(loginUserId);
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            String originalPasswordMd5 = MD5Util.MD5Encode(originalPassword, "UTF-8");
            String newPasswordMd5 = MD5Util.MD5Encode(newPassword, "UTF-8");
            //比较原密码是否正确
            if (originalPasswordMd5.equals(adminUser.getLoginPassword())) {
                //设置新密码并修改
                adminUser.setLoginPassword(newPasswordMd5);
                if (adminUserMapper.updateByPrimaryKeySelective(adminUser) > 0) {
                    //修改成功则返回true
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Boolean updateName(Integer loginUserId, String loginUserName, String nickName) {
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(loginUserId);
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            //修改信息
            adminUser.setLoginUserName(loginUserName);
            adminUser.setNickName(nickName);
            if (adminUserMapper.updateByPrimaryKeySelective(adminUser) > 0) {
                //修改成功则返回true
                return true;
            }
        }
        return false;
    }

    @Override
    public AdminUser register(String userName, String password) {
        String passwordMd5 = MD5Util.MD5Encode(password, "UTF-8");
        AdminUser user = new AdminUser();
        user.setLoginUserName(userName);
        user.setNickName(userName);
        user.setLoginPassword(passwordMd5);
        adminUserMapper.insertSelective(user);
        return user;
    }

    @Override
    public PageResult list(PageQueryUtil pageUtil) {
        List<AdminUser> categoryList = adminUserMapper.findUserList(pageUtil);
        int total = adminUserMapper.getTotalUsers(pageUtil);
        PageResult pageResult = new PageResult(categoryList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public boolean deleteBatch(Integer[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //修改tb_blog表
        adminUserMapper.delete(ids);
        return true;
    }

    @Override
    public Boolean saveBlogFriends(BlogFriends blogFriends) {
        return adminUserMapper.saveBlogFriends(blogFriends)>0;
    }

    @Override
    public PageResult getBlogFriendsPage(PageQueryUtil pageUtil) {
        List<BlogFriends> tags = adminUserMapper.findBlogFriendsList(pageUtil);
        int total = adminUserMapper.getTotalBlogFriends(pageUtil);
        PageResult pageResult = new PageResult(tags, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public boolean deleteBlogFriends(Integer[] ids) {
          adminUserMapper.deleteBlogFriends(ids) ;
          return true;
    }
}
